package com.saasovation.identityaccess.infrastructure.spring;

import com.saasovation.identityaccess.domain.model.DomainRegistry;
import com.saasovation.identityaccess.domain.model.access.AuthorizationService;
import com.saasovation.identityaccess.domain.model.access.RoleRepository;
import com.saasovation.identityaccess.domain.model.identity.*;
import com.saasovation.identityaccess.infrastructure.persistence.HibernateGroupRepository;
import com.saasovation.identityaccess.infrastructure.persistence.HibernateRoleRepository;
import com.saasovation.identityaccess.infrastructure.persistence.HibernateTenantRepository;
import com.saasovation.identityaccess.infrastructure.persistence.HibernateUserRepository;
import com.saasovation.identityaccess.infrastructure.services.MD5EncryptionService;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DomainConfiguration {
    @Bean
    public GroupRepository groupRepository(SessionFactory sessionFactory) {
        return new HibernateGroupRepository(sessionFactory);
    }

    @Bean
    public RoleRepository roleRepository(SessionFactory sessionFactory) {
        return new HibernateRoleRepository(sessionFactory);
    }

    @Bean
    public TenantRepository tenantRepository(SessionFactory sessionFactory) {
        return new HibernateTenantRepository(sessionFactory);
    }

    @Bean
    public UserRepository userRepository(SessionFactory sessionFactory) {
        return new HibernateUserRepository(sessionFactory);
    }

    @Bean
    public EncryptionService encryptionService() {
        return new MD5EncryptionService();
    }

    @Bean
    public GroupMemberService groupMemberService(UserRepository userRepository,
                                                 GroupRepository groupRepository) {
        return new GroupMemberService(userRepository, groupRepository);
    }

    @Bean
    public PasswordService passwordService() {
        return new PasswordService();
    }

    @Bean
    public TenantProvisioningService tenantProvisioningService(UserRepository userRepository,
                                                               RoleRepository roleRepository,
                                                               TenantRepository tenantRepository) {
        return new TenantProvisioningService(tenantRepository,
                userRepository,
                roleRepository);
    }

    @Bean
    public AuthenticationService authenticationService(UserRepository userRepository,
                                                       TenantRepository tenantRepository,
                                                       EncryptionService encryptionService) {
        return new AuthenticationService(tenantRepository,
                userRepository,
                encryptionService);
    }

    @Bean
    public AuthorizationService authorizationService(UserRepository userRepository,
                                                     GroupRepository groupRepository,
                                                     RoleRepository roleRepository) {
        return new AuthorizationService(userRepository,
                groupRepository,
                roleRepository);
    }

    @Bean
    public DomainRegistry domainRegistry() {
        return new DomainRegistry();
    }
}
